<template>
  <pro-layout
    :menus="menus"
    :collapsed="collapsed"
    :mediaQuery="query"
    :isMobile="isMobile"
    :handleMediaQuery="handleMediaQuery"
    :handleCollapse="handleCollapse"
    :i18nRender="i18nRender"
    :footerRender="false"
    :showHeader="showHeader"
    v-bind="settings"
  >
    <!--  LOGO 和 title 自定义 -->
    <template v-slot:menuHeaderRender>
      <div class="menu-logo">
        <!-- <img v-if="collapsed" style="width: 40px; height: 40px" src="@/assets/logo.png" alt=""> -->
        <div>
          <h1>{{ organization }}</h1>
          <span>{{ title }}</span>
        </div>
      </div>
    </template>
    <!--  增加 Header 左侧内容区自定义  -->
    <template v-slot:headerContentRender>
      <div>
        <a-tooltip title="刷新页面">
          <a-icon type="reload" style="font-size: 18px;cursor: pointer;" @click="updataPage" />
        </a-tooltip>
      </div>
    </template>
    <!--  系统设置setting -->
    <!-- <setting-drawer v-if="isShowSettings" :settings="settings" @change="handleSettingChange">
    </setting-drawer> -->
    <!--  头部右侧内容 -->
    <!-- <template v-slot:rightContentRender>
      <right-content :layout="settings.layout" :is-mobile="isMobile" :theme="settings.theme" />
    </template> -->
    <!-- custom footer / 自定义Footer -->
    <!--        <template v-slot:footerRender>-->
    <!--          <global-footer />-->
    <!--        </template>-->
    <router-view />
  </pro-layout>
</template>

<script>
import { i18nRender } from '@/locales'
import { mapState, mapActions } from 'vuex'
import { CONTENT_WIDTH_TYPE, SIDEBAR_TYPE, TOGGLE_MOBILE_TYPE } from '@/store/mutation-types'

import defaultSettings from '@/config/defaultSettings'

export default {
  name: 'BasicLayout',
  data () {
    return {
      showHeader: true,
      // end
      isShowSettings: defaultSettings.isShowSettings,
      // base
      menus: [],
      // 侧栏收起状态
      // collapsed: false,
      organization: defaultSettings.organization,
      title: defaultSettings.title,
      settings: {
        // 布局类型
        layout: defaultSettings.layout, // 'sidemenu', 'topmenu', 'top-left'
        // CONTENT_WIDTH_TYPE
        contentWidth: defaultSettings.layout === 'topmenu' ? defaultSettings.contentWidth : CONTENT_WIDTH_TYPE.Fluid,
        // 主题 'dark' | 'light'
        theme: defaultSettings.navTheme,
        // 主色调
        primaryColor: defaultSettings.primaryColor,
        fixedHeader: defaultSettings.fixedHeader,
        fixSiderbar: defaultSettings.fixSiderbar,
        colorWeak: defaultSettings.colorWeak,
        splitmenu: true,
        hideHintAlert: true,
        hideCopyButton: true
      },
      // 媒体查询
      query: {},
      // 是否手机模式
      isMobile: false
    }
  },
  computed: {
    ...mapState({
      // 动态主路由
      mainMenu: state => state.permission.addRouters,
      collapsed: state => state.app.sideCollapsed
    })
  },
  created () {
    this.GenerateRoutes()
    const routes = this.mainMenu.find(item => item.path === '/')
    this.menus = (routes && routes.children) || []
    // 处理侧栏收起状态
    this.$watch('collapsed', () => {
      this.$store.commit(SIDEBAR_TYPE, this.collapsed)
    })
    this.$watch('isMobile', () => {
      this.$store.commit(TOGGLE_MOBILE_TYPE, this.isMobile)
    })
  },
  mounted () {
    const userAgent = navigator.userAgent
    if (userAgent.indexOf('Edge') > -1) {
      this.$nextTick(() => {
        // this.collapsed = !this.collapsed
        this.$store.commit(SIDEBAR_TYPE, !this.collapsed)
        setTimeout(() => {
          // this.collapsed = !this.collapsed
          this.$store.commit(SIDEBAR_TYPE, !this.collapsed)
        }, 16)
      })
    }
    // 更新主题
    // updateTheme(this.settings.primaryColor)
  },
  methods: {
    i18nRender,
    ...mapActions([
      'GenerateRoutes'
    ]),
    updataPage () {
      this.$router.go(0)
    },
    handleMediaQuery (val) {
      this.query = val
      if (this.isMobile && !val['screen-xs']) {
        this.isMobile = false
        this.$store.commit(SIDEBAR_TYPE, false)
        return
      }
      if (!this.isMobile && val['screen-xs']) {
        this.isMobile = true
        this.$store.commit(SIDEBAR_TYPE, false)
        this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
      }
    },
    handleCollapse (val) {
      this.$store.commit(SIDEBAR_TYPE, val)
    },
    handleSettingChange ({ type, value }) {
      type && (this.settings[type] = value)
      switch (type) {
        case 'contentWidth':
          this.settings[type] = value
          break
        case 'layout':
          if (value === 'topmenu') {
            this.settings.fixSiderbar = false
            this.settings.contentWidth = defaultSettings.contentWidth
          } else {
            this.settings.contentWidth = 'Fluid'
          }
          if (value !== 'top-left' && this.settings.theme === 'bicolor') {
            this.settings.theme = 'dark'
          }
          this.$store.commit(SIDEBAR_TYPE, false)
          break
        // 修复黑暗模式下fixed的冲突
        case 'colorWeak':
          if (value) {
            document.querySelector('html').classList.add('colorWeak')
            this.$nextTick(() => {
              document.querySelector('#app .ant-pro-basicLayout').classList.remove('colorWeak')
            })
          } else {
            document.querySelector('html').classList.remove('colorWeak')
          }
          break
      }
    }
  }
}
</script>

<style lang="less">
@import './basic-layout.less';
</style>
